/*
 * Copyright (C) 2017 Jared Rummler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jaredrummler.ohos.colorpicker;

import com.jaredrummler.ohos.colorpicker.utils.ColorUtils;
import ohos.agp.components.*;
import ohos.agp.components.AttrHelper;
import ohos.agp.utils.Color;
import ohos.app.Context;

class ColorPaletteAdapter extends BaseItemProvider {

    /*package*/ final OnColorSelectedListener listener;
    /*package*/ final int[] colors;
    /*package*/ int selectedPosition;
    /*package*/ int colorShape;

    ColorPaletteAdapter(OnColorSelectedListener listener, int[] colors, int selectedPosition,
                        @ColorShape int colorShape) {
        this.listener = listener;
        this.colors = colors;
        this.selectedPosition = selectedPosition;
        this.colorShape = colorShape;
    }

    @Override
    public int getCount() {
        return colors.length;
    }

    @Override
    public Object getItem(int position) {
        return colors[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder(parent.getContext());
            convertView = holder.view;
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.setup(position);
        return convertView;
    }

    void selectNone() {
        selectedPosition = -1;
        notifyDataChanged();
    }

    interface OnColorSelectedListener {

        void onColorSelected(int color);
    }

    private final class ViewHolder {

        Component view;
        ColorPanelView colorPanelView;
        Text image;
        int originalBorderColor;

        ViewHolder(Context context) {
            int layoutResId;
            if (colorShape == ColorShape.SQUARE) {
                layoutResId = ResourceTable.Layout_cpv_color_item_square;
            } else {
                layoutResId = ResourceTable.Layout_cpv_color_item_circle;
            }
            view = LayoutScatter.getInstance(context).parse(layoutResId, null, false);
            colorPanelView = (ColorPanelView) view.findComponentById(ResourceTable.Id_cpv_color_panel_view);
            image = (Text) view.findComponentById(ResourceTable.Id_cpv_color_image_view);
            originalBorderColor = colorPanelView.getBorderColor();
            view.setTag(this);
        }

        void setup(int position) {
            int color = colors[position];
            int alpha = Color.alpha(color);
            colorPanelView.setColor(color);
            image.setText(selectedPosition == position ? "√" : "");
            if (alpha != 255) {
                if (alpha <= ColorPickerDialog.ALPHA_THRESHOLD) {
                    colorPanelView.setBorderColor(color | 0xFF000000);
                    image.setTextColor(Color.BLACK);
                } else {
                    colorPanelView.setBorderColor(originalBorderColor);
                    image.setTextColor(Color.WHITE);
                }
            } else {
                setColorFilter(position);
            }
            setClickedListener(position);
            int screenWidth = view.getContext().getResourceManager().getDeviceCapability().width;
            screenWidth = AttrHelper.vp2px(screenWidth, view.getContext());
            int padding = (screenWidth - 80 - AttrHelper.vp2px(50, view.getContext()) * 5) / 8;
            view.setPadding(padding, padding, padding, padding);
        }

        private void setClickedListener(final int position) {
            colorPanelView.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if (selectedPosition != position) {
                        selectedPosition = position;
                        notifyDataChanged();
                    }
                    listener.onColorSelected(colors[position]);
                }
            });
            colorPanelView.setLongClickedListener(new Component.LongClickedListener() {
                @Override
                public void onLongClicked(Component component) {
                    colorPanelView.showHint();
                }
            });
        }

        private void setColorFilter(int position) {
            if (position == selectedPosition)
                if (ColorUtils.calculateLuminance(colors[position]) >= 0.65) {
                    image.setTextColor(Color.BLACK);
                } else {
                    image.setTextColor(Color.WHITE);
                }
        }
    }
}